package org.example.typehandler;

import com.alibaba.fastjson.JSON;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.example.domain.StepInfo;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StepInfoTypeHandler extends BaseTypeHandler<StepInfo> {


    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, StepInfo parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, JSON.toJSONString(parameter));
    }

    @Override
    public StepInfo getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String str = rs.getString(columnName);
        return str == null ? null : JSON.parseObject(str, StepInfo.class);
    }

    @Override
    public StepInfo getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String str = rs.getString(columnIndex);
        return str == null ? null :JSON.parseObject(str, StepInfo.class);
    }

    @Override
    public StepInfo getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String str = cs.getString(columnIndex);
        return str == null ? null :JSON.parseObject(str, StepInfo.class);
    }
}
